package com.ruicar.afs.cloud.workflow.greatwall.processor.impl.msg;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.data.lock.anno.Lock;
import com.ruicar.afs.cloud.workflow.api.constant.FlowConstant;
import com.ruicar.afs.cloud.workflow.greatwall.dto.GwtRequest;
import com.ruicar.afs.cloud.workflow.greatwall.entity.AfsGreatWallFlowParams;
import com.ruicar.afs.cloud.workflow.greatwall.enums.GwtMsgType;
import com.ruicar.afs.cloud.workflow.greatwall.processor.AbsBaseProcessor;
import com.ruicar.afs.cloud.workflow.greatwall.processor.GwtFlowProcessor;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallFlowParamsService;
import com.ruicar.afs.cloud.workflow.greatwall.service.AfsGreatWallWorkflowBizDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class BizDataGetProcessor extends AbsBaseProcessor implements GwtFlowProcessor {
    private final StringRedisTemplate stringRedisTemplate;
    private final AfsGreatWallFlowParamsService afsGreatWallFlowParamsService;

    public BizDataGetProcessor(AfsGreatWallWorkflowBizDataService afsGreatWallWorkflowBizDataService, StringRedisTemplate stringRedisTemplate, AfsGreatWallFlowParamsService afsGreatWallFlowParamsService) {
        super(afsGreatWallWorkflowBizDataService);
        this.stringRedisTemplate = stringRedisTemplate;
        this.afsGreatWallFlowParamsService = afsGreatWallFlowParamsService;
    }

    @Override
    public GwtMsgType msgType() {
        return GwtMsgType.GET_BIZ_DATA;
    }

    @Override
    @Lock(keyConstant = ":lock-afs-biz-data-request", keys = {"#req.getLockKey()"})
    public String process(GwtRequest req) {
        log.info("获取业务数据{}", req);
        String value = stringRedisTemplate.opsForValue().get(FlowConstant.templateKey(req.getServiceId(), req.getDataId()));
        if (value == null) {
            value = afsGreatWallWorkflowBizDataService.getBizDataByServiceIdAndDataId(req.getServiceId(), req.getDataId());
        }
        if (value == null) {
            throw new AfsBaseException("找不到业务数据");
        }
        JSON json = JSONUtil.parse(value);
        List<AfsGreatWallFlowParams> paramsList = afsGreatWallFlowParamsService.list(Wrappers.<AfsGreatWallFlowParams>lambdaQuery().eq(AfsGreatWallFlowParams::getGreatWallServiceId, req.getServiceId()));
        JSONObject rtnObject = new JSONObject();
        for (AfsGreatWallFlowParams afsGreatWallFlowParams : paramsList) {
            rtnObject.put(afsGreatWallFlowParams.getParamKey(), JSONUtil.getByPath(json, afsGreatWallFlowParams.getParamValueExpress()));
        }
        log.info("返回给流程引擎业务数据是===>{}", rtnObject.toJSONString());
        return rtnObject.toJSONString();
    }
}
